//
// Created by baigui on 2021/11/19.
//

#ifndef SIMPLE_PLAYER_MKVNODEBASE_H
#define SIMPLE_PLAYER_MKVNODEBASE_H

#include <cstdint>
#include <BaseStream.h>
#define EBML_ID_HEADER             0x1A45DFA3
static uint64_t getLength(uint8_t * buf, int maxLengt, int *size){
    /* length of header */
    uint64_t  total = buf[0];
    int len_mask = 0x80, n = 1;
    while (*size <= maxLengt && !(total & len_mask)) {
        (*size)++;
        len_mask >>= 1;
    }
    if (*size > maxLengt)
        return 0;
    total &= (len_mask - 1);
    while (n < *size)
        total = (total << 8) | buf[n++];
    return total;
}
class MKVNodeBase {
public:
    MKVNodeBase(uint32_t id, BaseStream *stream,uint64_t basePostion){
        mBasePostion = basePostion;
        mID = id;
        mStream = stream;
    }

    virtual bool process() = 0;
    bool getBasicInfo();
    uint64_t getNodeSize(){
        return mBasicLength + mLength;
    }

protected:
    uint64_t mBasePostion;
    uint64_t mPostion;
    uint64_t mLength;
    int mBasicLength;
    uint32_t mID;
    BaseStream *mStream = nullptr;
    MKVNodeBase *mParent = nullptr;
    MKVNodeBase *mNext = nullptr;
};

class HeaderNode : public MKVNodeBase{
public:
    HeaderNode(BaseStream *stream,uint64_t basePostion);
    bool process() override;
};

class ContentNode : public MKVNodeBase{
public:
    ContentNode(BaseStream *stream, uint64_t basePostion);
    bool process() override;
};

#endif //SIMPLE_PLAYER_MKVNODEBASE_H
